import { BadGatewayException, Body, Controller, Ip, Post, Req } from '@nestjs/common';
import { ModelType } from '@typegoose/typegoose/lib/types';
import { InjectModel } from 'nestjs-typegoose';
import { JihuoModel } from 'src/mongodb/models/jihuoModel/jihuoModel';
import { OperatService } from '../../operatApi/operat.service';
import * as crypto from 'crypto'
import { LanxiModel } from 'src/mongodb/models/lianxiModel/LanxiModel';
import { QudaobaoModel } from 'src/mongodb/models/qudaobaoModel/qudaobaoModel';

@Controller('index')
export class FuzhiController {


    constructor(
        @InjectModel(JihuoModel) private readonly jihuo: ModelType<JihuoModel>,
        @InjectModel(LanxiModel) private readonly lianxi: ModelType<LanxiModel>,
        @InjectModel(QudaobaoModel) private readonly qudaobao: ModelType<QudaobaoModel>,
        private readonly operat: OperatService,
    ) { }


    /* 点击复制*/
    @Post('clickCopy')
    async clickFuzhi(@Body() body, @Req() req, @Ip() ip) {
        try {
            if (body.imei) {
                const md5 = crypto.createHash('md5');
                const imeimd5 = md5.update(body.imei).digest('hex')
                try {
                    const channelResult = await this.qudaobao.findOne({ mark: req.dns })
                    /* web模式添加ip信息 */
                    if (channelResult.webmodel) {
                        let parse = JSON.parse(body.imei)
                        parse.ips = ip.match(/\d+.\d+.\d+.\d+/)[0]
                        body.imei = JSON.stringify(parse)
                    }
                    /* 是否加白 */
                    if (!channelResult.whitelist) {
                        /* 联系方式是否去重  去重复制次数只记录一次,没有去重点击一次记录一次*/
                        if (channelResult.deduplication) {
                            const FuzhiData = await this.jihuo.findOne({ ditch: req.dns, imeiMd5: imeimd5, type: 'fuzhi' })
                            if (!FuzhiData) {
                                /* 统计渠道包复制次数 */
                                await this.lianxi.updateOne({ _id: body.id, status: true }, { $inc: { todaycopy: 1, totalcopy: 1 } })
                                await this.qudaobao.updateOne({ mark: req.dns }, { $inc: { todaycopy: 1, totalcopy: 1 } })

                                /* 存用户设备信息 */
                                await this.jihuo.create({
                                    ditch: req.dns,
                                    imeiMd5: imeimd5,
                                    imei: body.imei,
                                    visitorid: body.visitorId,
                                    platform: body.platform,
                                    createtime: Math.floor(Date.now() / 1000),
                                    type: 'fuzhi',
                                    userid: channelResult.userid,
                                    username: channelResult.username
                                })

                                await this.operat.methodFun(body, req, 'fuzhi')
                            } else {
                                console.log('设备重复复制');
                            }
                        } else {
                            /* 统计渠道包复制次数 */
                            await this.lianxi.updateOne({ _id: body.id, status: true }, { $inc: { todaycopy: 1, totalcopy: 1 } })
                            await this.qudaobao.updateOne({ mark: req.dns }, { $inc: { todaycopy: 1, totalcopy: 1 } })

                            /* 存用户设备信息 */
                            await this.jihuo.create({
                                ditch: req.dns,
                                imeiMd5: imeimd5,
                                imei: body.imei,
                                visitorid: body.visitorId,
                                platform: body.platform,
                                createtime: Math.floor(Date.now() / 1000),
                                type: 'fuzhi',
                                userid: channelResult.userid,
                                username: channelResult.username
                            })

                            await this.operat.methodFun(body, req, 'fuzhi')
                        }
                    } else {
                        await this.operat.methodFun(body, req, 'fuzhi')
                        console.log('复制加白联调');
                    }
                } catch (error) {
                    throw error
                }
            } else {
                console.log("复制异常" + JSON.stringify(body));
                throw '复制手机环境异常,请使用正规手机端打开app哟,或者重启app再试哟'
            }

        } catch (error) {
            console.log(error);
            throw new BadGatewayException(error)
        }
    }


    /* 点击跳转失败 */
    @Post('clicktz')
    async clicktz(@Body() body, @Req() req) {
        try {
            await this.lianxi.updateOne({ _id: body.id, status: true }, { $inc: { todaynum: 1, todaycopy: -1, totalcopy: -1 } })
            await this.qudaobao.updateOne({ mark: req.dns }, { $inc: { todaytiaozhuan: 1, todaycopy: -1, totalcopy: -1 } })
        } catch (error) {
            console.log(error);
            throw new BadGatewayException(error)
        }
    }
}
